//
//  UnicodeViewController.swift
//  JKTools
//
//  Created by 姜奎 on 2024/4/24.
//

import Cocoa

class StringViewController: NSViewController {

    @IBOutlet var inputTextView: NSTextView!
    
    @IBOutlet var outputTextView: NSTextView!
    
    @IBOutlet weak var tableView: NSTableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        
        let nib = NSNib(nibNamed: "StringTransformCell", bundle: nil)
        tableView.register(nib, forIdentifier: NSUserInterfaceItemIdentifier("StringTransform"))
        tableView.reloadData()
        tableView.selectRowIndexes(IndexSet(integersIn: 0..<1), byExtendingSelection: true)
    }
    
    @IBAction func transform(_ sender: Any) {
        let inputString = inputTextView.string
        let outputString = outputTextView.string
        let transform = StringTransform.transforms[tableView.selectedRow]
        
        if inputString.isEmpty == false {
            let output = transform.transformInput(inputString)
            guard output.isEmpty == false  else { return }
            outputTextView.string = output
            return
        }
        
        if outputString.isEmpty == false {
            let input = transform.transformOutput(outputString)
            guard input.isEmpty == false  else { return }
            inputTextView.string = input
            return
        }
        
    }
}

extension StringViewController: NSTableViewDelegate,NSTableViewDataSource {
    func numberOfRows(in tableView: NSTableView) -> Int {
        return StringTransform.transforms.count
    }
    
    func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
        return 25.0
    }
    
    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let identifier = NSUserInterfaceItemIdentifier("StringTransform")
        
        let cell = tableView.makeView(withIdentifier: identifier, owner: self) as! StringTransformCell
        
        
        cell.textField?.stringValue = StringTransform.transforms[row].rawValue

        return cell
    }
 
    func tableView(_ tableView: NSTableView, shouldEdit tableColumn: NSTableColumn?, row: Int) -> Bool {//可进行编辑
        return false
    }
    func tableView(_ tableView: NSTableView, toolTipFor cell: NSCell, rect: NSRectPointer, tableColumn: NSTableColumn?, row: Int, mouseLocation: NSPoint) -> String {//鼠标悬停在cell上显示的提示文本
        return StringTransform.transforms[row].rawValue
    }
    func tableView(_ tableView: NSTableView, shouldShowCellExpansionFor tableColumn: NSTableColumn?, row: Int) -> Bool {//当列表长度无法展示完整某行数据时 当鼠标悬停在此行上 是否扩展显示
        return true
    }
    func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
        print("tableColumn.title", tableColumn.title)
        
    }
    func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
        print("row:", row)
        
        return true
    }
}

enum StringTransform: String {
    case url = "URL编码<->解码"
    case unixTimestamp = "Unix时间戳->日期"
    
    static let transforms:[StringTransform] = [.url,.unixTimestamp]
    
    func transformInput(_ input: String) -> String {
        
        switch self {
        case .url:
            return input.URLEncode()
        case .unixTimestamp:
            return input.unixFormat()
        }
    }
    
    func transformOutput(_ output: String) -> String {
        
        switch self {
        case .url:
            return output.URLDecode()
        case .unixTimestamp:
            return output.unixTimestamp()
        }
    }
}

